/*
Author : Nguyen Van Ty
Email  : ngvanty03@gmail.com
Date   : 12/13/2010 

*/

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Utility;
using BusinessObject;
using System.Xml;

namespace DataAccessLayer
{
    /// <summary>
    /// Define UserProfileDao class. This class inherit from  UserProfileDaoBase class.
    /// If you want add new function, you must define here.
    /// </summary>
    public class UserProfileDao : UserProfileDaoBase
    {
        private static ILog logger = null;
        #region Constructor
        /// <summary>
        /// Define Constructor.
        /// </summary>
        public UserProfileDao() { }
        static UserProfileDao()
        {
            // Init logger.
            logger = LoggingUtility.GetLogger(typeof(UserProfileDao).FullName);
        }
        public static UserProfile Get(Guid userId,string application)
        {
            String methodName = "GetUserProfile";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                UserProfile result = null;
                DbCommand dbCommnand = ManagerDao.GetStoredProcCommand("spGetUserProfile");
                //Add value "UserId"  paramater.
                ManagerDao.AddInParameter(dbCommnand, "UserId", DbType.Guid, userId);
                //Add value "UserId"  paramater.
                ManagerDao.AddInParameter(dbCommnand, "ApplicationName", DbType.String, application);

                IDataReader reader = ManagerDao.ExecuteReader(dbCommnand);
                if (reader.Read())
                {
                    result = GetUserProfileByDataReader(reader);

                }
                reader.Close();
                return result;
            }
            catch (System.Exception e)
            {
                //write error message to log file.
                logger.Error("Get", e);
                return null;
            }
        }
        public static Vw_aspnet_Roles GetMaxRole(Guid userId, string application)
        {
            String methodName = "GetMaxRole";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                Vw_aspnet_Roles result = null;
                DbCommand dbCommnand = ManagerDao.GetStoredProcCommand("spMaxRoleForUser");
                //Add value "UserId"  paramater.
                ManagerDao.AddInParameter(dbCommnand, "UserId", DbType.Guid, userId);
                //Add value "UserId"  paramater.
                ManagerDao.AddInParameter(dbCommnand, "ApplicationName", DbType.String, application);

                IDataReader reader = ManagerDao.ExecuteReader(dbCommnand);
                if (reader.Read())
                {
                    result = GetRoleByDataReader(reader);

                }
                reader.Close();
                return result;
            }
            catch (System.Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        public static Vw_aspnet_Roles GetRoleByName(string application,string roleName)
        {
            String methodName = "GetRoleByName";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                Vw_aspnet_Roles result = null;
                DbCommand dbCommnand = ManagerDao.GetStoredProcCommand("spGetRoleByName");
                //Add value "UserId"  paramater.
                ManagerDao.AddInParameter(dbCommnand, "RoleName", DbType.String, roleName);
                //Add value "UserId"  paramater.
                ManagerDao.AddInParameter(dbCommnand, "ApplicationName", DbType.String, application);

                IDataReader reader = ManagerDao.ExecuteReader(dbCommnand);
                if (reader.Read())
                {
                    result = GetRoleByDataReader(reader);

                }
                reader.Close();
                return result;
            }
            catch (System.Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        protected static Vw_aspnet_Roles GetRoleByDataReader(IDataReader reader)
        {
            String methodName = "GetRoleByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                Vw_aspnet_Roles result = new Vw_aspnet_Roles();
                //read UserId field data.

                result.Level = ManagerDao.GetintDataFromDataReader(reader, "Level");
                //read Name field data.

                result.MenuFile = ManagerDao.GetstringDataFromDataReader(reader, "MenuFile");
                result.DefaultUrl = ManagerDao.GetstringDataFromDataReader(reader, "DefaultUrl");
                result.Description = ManagerDao.GetstringDataFromDataReader(reader, "Description");
                result.RoleName = ManagerDao.GetstringDataFromDataReader(reader, "RoleName");     

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
    }
}
        #endregion